#!/bin/bash

# ----------------------------------------------------------------------
# | Helper Functions                                                   |
# ----------------------------------------------------------------------

enable_programmable_completion() {

    # Enable programmable completion features
    # http://tldp.org/LDP/abs/html/tabexpansion.html

    if [ "$OS" == "osx" ]; then
        if [ -f $(brew --prefix)/share/bash-completion/bash_completion ]; then

            source $(brew --prefix)/share/bash-completion/bash_completion

            # Make bash complete the `g` alias, just like it does `git`
            complete -o default -o nospace -F _git g

        fi

    elif [ "$OS" == "ubuntu" ]; then
        if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
            source /etc/bash_completion
        fi
    fi

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    [ -f "$NVM_DIR/bash_completion" ] \
        && source $NVM_DIR/bash_completion

}

get_os() {

    declare -r OS_NAME="$(uname -s)"
    local os=''

    if [ "$OS_NAME" == "Darwin" ]; then
        os='osx'

    # Get Linux distribution
    # https://web.archive.org/web/20130929034731/http://linuxmafia.com/faq/Admin/release-files.html
    elif [ "$OS_NAME" == "Linux" ] && \
         [ -e "/etc/lsb-release" ]; then
        os='ubuntu'
    fi

    printf "%s" "$os"
}

source_bash_files() {

    local file=''
    local i=''

    declare -r -a FILES_TO_SOURCE=(
        'bash_aliases'
        'bash_exports'
        'bash_functions'
        'bash_options'
        'bash_prompt'
        'bash.local'  # for local settings that should
                      # not be under version control
    )

    for i in ${!FILES_TO_SOURCE[*]}; do
        file="$HOME/.${FILES_TO_SOURCE[$i]}"
        [ -r "$file" ] && source "$file"
    done

}

# ----------------------------------------------------------------------
# | Main                                                               |
# ----------------------------------------------------------------------

main() {

    # The presence of the OS variable in this scope is important as
    # the following functions rely on it (e.g. the sourced files use
    # it to determine what to execute)
    declare -r OS="$(get_os)"

    source_bash_files
    enable_programmable_completion

}

main

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Unset functions in order to not pollute the global space
unset -f enable_programmable_completion
unset -f get_os
unset -f main
unset -f source_bash_files

# Clear system messages (e.g.: system copyright notice, the
# date and time of the last login, the message of the day, etc.)
clear
